---
title: "Merge Permutation And Vreeland and Dreher files"
author: "Gina Reynolds and Matthew Winters"
output: pdf_document
---

This file is used in replicating findings for "Foreign Aid Funnel? Placebo-Based Assessment of Aid Flows to  Non-Permanent U.N. Security Council Members" by Evangeline Reynolds (University of Illinois) and Matthew Winters (University of Illinois). 

```{r}
library(foreign)
Ch5=read.dta("./01VreelandAndDreher/VreelandAndDreherChapter5.dta")
VariableKey=data.frame(Var=names(Ch5), attr(Ch5, "var.labels"))
dim(Ch5)
names(Ch5)[1:12]
dim(unique(Ch5[c("code", "year")]))


head(VariableKey);tail(VariableKey)
load("05_LongSCPermutations.RData")
ls()
head(names(LongSCPermutations))
tail(names(LongSCPermutations))
#head(LongSCPermutations[1:7])

```


Standardize Names for Each Country
===

```{r}
setwd("./000MyCountryCoder/") #For chunk temporarily resets locaction
source("countrycode2.R")
Ch5$StandardName=FavoriteCountryName(Ch5$aclpname)
min(Ch5$year); max(Ch5$year)
dim(unique(Ch5[c("StandardName", "year")]))
```

```{r,cache=FALSE}
head(Ch5[1:5])
head(LongSCPermutations[1:5])
dim(LongSCPermutations)
LongSCPermutations[c("Permutation1", "Year")][duplicated(LongSCPermutations[c("Permutation1", "Year")]),]
LongSCPermutations=
  LongSCPermutations[LongSCPermutations$WhichTenureYear!=3,]
head(LongSCPermutations[1:5])
PermutationNames=names(LongSCPermutations)[which(names(LongSCPermutations)=="Permutation1"):ncol(LongSCPermutations)]
NumPermutation=length(PermutationNames)
LongSCPermutations[c("Year", "WhichTenureYear", "RealizedAssignment")][duplicated(LongSCPermutations[c("Year", "WhichTenureYear", "RealizedAssignment")]),]
```

```{r}
#Consistency check 
Merge=merge(LongSCPermutations[c("Year", 
                           "WhichTenureYear", 
                           "RealizedAssignment")],  
      Ch5[c("year", "StandardName", "unsc")], 
      by.x=c("Year", "RealizedAssignment"), 
      by.y=c("year", "StandardName"), all=TRUE
)

dim(Merge)
min(Ch5$year);max(Ch5$year)
Merge=Merge[Merge$Year>=1951&Merge$Year<=2009,]
dim(Merge)
```

Merging
===

Our initial data processing does not produce exactly the same information about UN/UNSC memberships as found in Vreeland and Dreher's replication data. 
For example, Vreeland and Dreher code Ethiopia as NA in their data across a period when that country is on the Security Council.  We follow Vreeland and Dreher in their exclusion of country-years to make a direct comparison with their results.

```{r}
head(Merge)

table(Merge$WhichTenureYear, Merge$unsc, useNA="always")

subset(Merge, Merge$unsc==1&is.na(Merge$WhichTenureYear))

subset(Merge, is.na(Merge$unsc)&
         Merge$WhichTenureYear %in% c(1,2))
```



```{r}
dim(Ch5)
#Ch5=Ch5[c("year","StandardName", "code", "unsc")]
dim(Ch5)
 for (i in 1:NumPermutation){

  WhichPermutation=paste0("Permutation",i)
   
 Ch5=merge(Ch5,
           LongSCPermutations[c("Year","WhichTenureYear", WhichPermutation)], 
           by.x=c("year","StandardName"),
           by.y=c("Year", WhichPermutation ),
           all.x=TRUE
           )
 
  Ch5$WhichTenureYear[is.na(Ch5$WhichTenureYear)]=0
  Ch5$WhichTenureYear[Ch5$WhichTenureYear==1|
                      Ch5$WhichTenureYear==2]=1
  
  # Overwrite this var with NAs when Vreeland and Dreher do so..
  Ch5$WhichTenureYear[is.na(Ch5$unsc)]=NA
 
  names(Ch5)[names(Ch5)=="WhichTenureYear"]=paste0("SCReassign",i)
 
 }
head(names(Ch5))
tail(names(Ch5))
```

Save the Data with Reassignments for Security Council Compositions.
==


```{r}
Ch5_Temp=Ch5[c("year","StandardName", "code", "unsc", paste0("SCReassign",1:NumPermutation))]
write.dta(dataframe=Ch5_Temp, file="07_Ch5wPerm.dta")
```


